package tcm.com.gistone.database.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import tcm.com.gistone.database.entity.WordRelation;

import java.util.List;
import java.util.Map;


public interface WordRelationMapper {

    /*插入*/
    @Insert("INSERT INTO tb_a#{a}_b#{b}(word_id,ano_word_id,book_id,num,sec_type) VALUES(#{wr.wordId}, #{wr.anoWordId}, #{wr.bookId}, #{wr.num},#{wr.sectionType})")
    void insert(@Param("a") long a, @Param("b") long b, @Param("wr") WordRelation wr);


    @Select("Select * from tb_a#{a}_b#{b} where book_id = #{bookId} and word_id = #{word1} and ano_word_id = #{word2} and sec_type = #{sectionType}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "wordId", column = "word_id"),
            @Result(property = "anoWordId", column = "ano_word_id"),
            @Result(property = "bookId", column = "book_id"),
            @Result(property = "num", column = "num"),
            @Result(property = "sectionType", column = "sec_type")
    })
     WordRelation getByWords(@Param("a") long a, @Param("b") long b, @Param("word1") long word1, @Param("word2") long word2, @Param("bookId") long bookId, @Param("sectionType") String sectionType);

    @Select("SELECT count(*) FROM tb_a#{a}_b#{b} where word_id = #{wordId}")
     int selectCountByWord(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId);


    @Select("SELECT  ano_word_id as wordId,sum(num) FROM tb_a#{a}_b#{b} where word_id = #{wordId} GROUP BY wordId")
    List<Map> selectByWord(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId);

    @Select("SELECT  ano_word_id as wordId,sum(num) FROM tb_a#{a}_b#{b} a join (select * from tb_special_book where special_id = #{specialId}) b on a.book_id = b.book_id where word_id = #{wordId} GROUP BY wordId")
    List<Map> selectByWordWithSpecial(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId, @Param("specialId") long specialId);

    @Select("SELECT  ano_word_id as wordId,num FROM tb_a#{a}_b#{b} where  word_id = #{wordId} and book_id = #{bookId}")
    List<Map> selectByWordWithBook(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId, @Param("bookId") long bookId);

    @Select("SELECT word_id as wordId,sum(num) FROM tb_a#{a}_b#{b} a join (select * from tb_special_book where special_id = #{specialId}) b on a.book_id = b.book_id where ano_word_id = #{wordId} GROUP BY wordId")
    List<Map> selectByAnoWordWithSpecial(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId, @Param("specialId") long specialId);

    @Select("SELECT word_id as wordId,sum(num) FROM tb_a#{a}_b#{b} where ano_word_id = #{wordId} GROUP BY wordId ")
    List<Map> selectByAnoWord(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId);

    @Select("SELECT  word_id as wordId,num FROM tb_a#{a}_b#{b} where ano_word_id = #{wordId} and book_id = #{bookId}")
    List<Map> selectByAnoWordWithBook(@Param("a") long a, @Param("b") long b, @Param("wordId") long wordId, @Param("bookId") long bookId);

    /*更新记录*/
    @Update("update tb_a#{a}_b#{b} set num = #{wr.num} where id = #{wr.id}")
     int update(@Param("a") long a, @Param("b") long b, @Param("wr") WordRelation wr);

}
